<template>
  <div class="login-layer">
    <div class="login-content">
      <div class="login-logo">
        <div class="logo"><img src="~@/assets/login/logo.png" alt="" /></div>
        <h2>欢迎登录</h2>
        <h3>我的管理系统</h3>
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="rules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <el-form-item prop="account">
          <el-input
            ref="account"
            v-model="loginForm.account"
            placeholder="请输入手机号"
            type="text"
            maxlength="11"
            auto-complete="on"
          >
            <svg-icon
              slot="prefix"
              style="marginleft: 4px; marginright: 4px"
              icon-class="person"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入账号密码"
            name="password"
            auto-complete="on"
            size="medium"
            maxlength="16"
            @keyup.enter.native="codeSubmit"
          >
            <svg-icon
              slot="prefix"
              style="marginleft: 4px; marginright: 4px"
              icon-class="password"
            />
          </el-input>
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              style="color: #c0c2d1"
              :icon-class="passwordType === 'password' ? 'hide' : 'display'"
            />
          </span>
        </el-form-item>
        <el-button
          :loading="loading"
          type="primary"
          class="login-Btn_in"
          @click.native.prevent="codeSubmit"
        >
          登<span style="display: inline-block; width: 20px" />录
        </el-button>
        <div class="register-box">
          <div class="register-unit">
            还不是已注册单位？<a href="javascript:;" @click="registerUnit"
              >点击申请</a
            >
          </div>
          <div class="no-account">
            <a href="javascript:;" @click="forgetPassword">忘记密码？</a>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { checkPhone } from "@/utils/validate";
export default {
  name: "adminLogin",
  data() {
    // 密码校验
    const validatePassword = (rule, value, callback) => {
      let reg = /^(?=.*\d)(?=.*[a-zA-Z])[\da-zA-Z~!@#$%^&*]{8,16}$/;
      if (!value) {
        callback(new Error("请输入密码"));
      }
      if (value && !reg.test(value)) {
        callback(new Error("请确认密码格式是否正确"));
      } else {
        callback();
      }
    };
    // 手机号校验
    const validatePhone = (rule, value, callback) => {
      if (!value) {
        callback("请输入手机号");
      } else if (!checkPhone(value)) {
        callback("请输入正确的手机号");
      } else {
        callback();
      }
    };
    return {
      loading: false,
      passwordType: "password",
      loginForm: {
        account: "",
        password: "",
      },
      rules: {
        account: [
          {
            required: true,
            trigger: "blur",
            validator: validatePhone,
          },
        ],
        password: [
          {
            required: true,
            trigger: "blur",
            validator: validatePassword,
          },
        ],
      },
    };
  },
  methods: {
    registerUnit() {},
    forgetPassword() {},
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    codeSubmit() {
      this.$refs["loginForm"].validate((valid) => {
        if (valid && this.loginForm.account && this.loginForm.password) {
          this.$message.success('登陆成功！')
          this.$router.push("/unitManagement/unitMembers");
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" >
* {
  padding: 0;
  margin: 0;
}
.login-layer {
  width: 1100px;
  min-height: 100vh;
  background: url(~@/assets/login/bg2x.png) no-repeat center;
  background-size: cover;
  .login-content {
    position: absolute;
    right: 130px;
    bottom: 170px;
    width: 440px;
    height: 500px;
    background: #ffffff;
    box-shadow: 0px 2px 24px 13px rgba(64, 64, 64, 0.1);
    border-radius: 16px;
    padding: 45px 60px 64px 60px;
    z-index: 100;
  }
  .el-input {
    display: inline-block;
    height: 54px;
    width: 440px;
    input {
      height: 100%;
    }
  }
  .login-logo {
    text-align: center;
    align-items: center;
    .logo {
      width: 76px;
      height: 76px;
      margin: 0 auto;
      img {
        width: 100%;
        height: 100%;
      }
    }
    h2 {
      font-size: 20px;
      margin-top: 12px;
      margin-bottom: 0px;
    }
    h3 {
      font-size: 16px;
      margin-top: 0px;
      margin-bottom: 27px;
    }
    h2,
    h3 {
      color: #333;
      font-weight: 500;
    }
  }
  .login-Btn_in {
    width: 100%;
    height: 54px;
    line-height: 8px;
    background: #39aab5;
    border-radius: 8px;
    box-shadow: 0px 4px 30px 0px rgba(119, 138, 199, 0.08);
    margin-bottom: 16px;
    margin-top: 18px;
    font-size: 18px;
  }
  .login-form {
    width: 100%;
    .el-input__prefix,
    .el-input__suffix {
      display: flex;
      align-items: center;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 10px;
    font-size: 16px;
    color: #889aa4;
    cursor: pointer;
    user-select: none;
  }
  .register-box {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    font-weight: 400;
    color: #666666;
    a {
      color: #1a5eff;
    }
  }
}
</style>